package com.authorization.service.impl;

import com.authorization.mapper.UserDetailMapper;
import com.authorization.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @author chengyadong
 * @date 2023/11/30 13:39
 * @description 重新定义loadUserByUsername方法，从数据库比较用户名
 * 新版的只需要实现注入到ioc容器中即可生效
 * 旧版的我们实现后还需要进行手动配置 AuthenticationManagerBuilder auth.userDetailsService(UserDetailServiceImpl)
 */
@Component
public class UserDetailServiceImpl implements UserDetailsService {
	@Autowired
	UserDetailMapper userDetailMapper;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		UserInfo user = userDetailMapper.loadUserByUsername(username);
		if (user == null) {
			throw new UsernameNotFoundException("当前用户不存在");
		}
		return user;
	}
}
